package com.mobinters.cafeman.module;

import java.util.ArrayList;
import java.util.List;

import com.mobinters.cafeman.CMUtils;
import com.mobinters.cafeman.R;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SlidingDrawer;
import android.widget.SlidingDrawer.OnDrawerCloseListener;
import android.widget.SlidingDrawer.OnDrawerOpenListener;
import android.widget.Spinner;

import com.mobinters.cafeman.CMActivity;
import com.mobinters.cafeman.adapter.CProductAdapter;
import com.mobinters.cafeman.adapter.TableAdapter;
import com.mobinters.cafeman.object.Product;
import com.mobinters.cafeman.object.Table;

public class MainSlideModule implements OnClickListener{

	CMActivity mActivity;
	public boolean isSlideDrawerOpen=false;
	private SlidingDrawer mainSlidingDrawer;
	ListView mListView; //order lis
	public static CProductAdapter OrderAdapter;
	List<Product> dataList;
	SharedPreferences mPref;
	Editor mEditor;
	
	
	@SuppressLint("NewApi")
	public MainSlideModule(CMActivity activity){
		mActivity =  activity;
		
		mPref = mActivity.getSharedPreferences(CMUtils.PREF_NAME , 0);
		mEditor = mPref.edit();
		
		mListView = (ListView) mActivity.findViewById(R.id.lv_order);
		
		
		
		mainSlidingDrawer=(SlidingDrawer)mActivity.findViewById(R.id.slidingDrawer);
		mainSlidingDrawer.setOnDrawerCloseListener(new OnDrawerCloseListener(){

			@Override
			public void onDrawerClosed() {
				isSlideDrawerOpen=false;
				mainSlidingDrawer.setClickable(false);
			}
		});
		mainSlidingDrawer.setOnDrawerOpenListener(new OnDrawerOpenListener(){

			@Override
			public void onDrawerOpened() {
				isSlideDrawerOpen=true;
				mainSlidingDrawer.setClickable(true);
				/*
				 * Load checked List
				 */
				//Log.e("data size",MealLoadModule.ProductList.size() + "");
				//mOrderAdapter.notifyDataSetChanged();
				dataList = MealLoadModule.ProductList;
				if(dataList!=null &&  dataList.size() != 0)										
				{   
					
					OrderAdapter = new CProductAdapter(mActivity, dataList);
					mListView.setAdapter(OrderAdapter);			
					
				}
			}
		});
		
		mActivity.findViewById(R.id.btn_slide_order).setOnClickListener(this);
		mActivity.findViewById(R.id.btn_slide_cancel).setOnClickListener(this);
		
	}
	
	public void closeSlidingDrawer(){
		isSlideDrawerOpen=false;
		mainSlidingDrawer.close();
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch(id){	
			case R.id.btn_slide_order:
				showOrderDialog(mActivity);
			break;
		}
	}
	
	
	private void showOrderDialog(Context context){
		final Dialog dialog = new Dialog(context);
		dialog.setTitle("Order");
		dialog.setContentView(R.layout.dlg_order);
		dialog.setCancelable(false);
		final EditText txtOrderName = (EditText) dialog.findViewById(R.id.txt_order_name);
		txtOrderName.setText(mPref.getString(CMUtils.PREF_KEY_USERNAME, ""));
		txtOrderName.setEnabled(false);
		
		dialog.findViewById(R.id.btn_order_login).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				txtOrderName.setEnabled(true);
				//set new sharepreference
			}
		});
		
		dialog.findViewById(R.id.btn_order_next).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				showTableDialog(mActivity);
			}
		});
		
		dialog.show();
	}
	
	private void showTableDialog(Context context){
		final Dialog dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dlg_table);
		dialog.setCancelable(false);
		Spinner spinArea = (Spinner) dialog.findViewById(R.id.spin_table_area);
		
		SharedPreferences mPref = context.getSharedPreferences(CMUtils.PREF_NAME, 0);
		String areaContent = mPref.getString(CMUtils.PREF_KEY_AREA, null);
		if(areaContent != null) {
				String[] areaArray = areaContent.split(",");
				List<String> areaList = null;
				if(areaArray!=null && areaArray.length > 0){
						areaList = new ArrayList<String>();
						for(int i = 0 ; i < areaArray.length ; i++) areaList.add(areaArray[i]);
				}
				
				if(areaList != null){
					
						ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(context, 
								android.R.layout.simple_spinner_item, areaList);
						dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
						spinArea.setAdapter(dataAdapter);
				}						
								
		}
		createTableData();
		GridView tableList = (GridView) dialog.findViewById(R.id.grid_table);
		if(mTableData != null){
				TableAdapter adapter = new TableAdapter(mActivity, mTableData);
				tableList.setAdapter(adapter);
		}
		
		dialog.findViewById(R.id.btn_table_cancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				
			}
		});
		
		dialog.findViewById(R.id.btn_table_order).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dialog.dismiss();
				
			}
		});
		
		dialog.show();
	}
	
	List<Table> mTableData;
	private void createTableData(){
		mTableData = new ArrayList<Table>();
		Table item;
		for(int j = 0; j < 3; j++){
			item = new Table();
			item.Id = j+1;
			item.Name = "Ban " + (j+1);
			item.Checked = false;
			item.Available = false;
			mTableData.add(item);
		}
		for(int i = 3 ; i < 7; i++){
			item = new Table();
			item.Id = i+1;
			item.Name = "Ban " + (i+1);
			item.Checked = false;
			item.Available = true;
			mTableData.add(item);
		}
		
		item = new Table();
		item.End = true;
		item.Id = 8;
		item.Name = "Ban " + 8;
		mTableData.add(item);
		
	}
	
}
